package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.domain.Order;
import com.itheima.domain.Product;
import com.itheima.service.OrderService;
import com.itheima.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;


/**
 * @author MI
 * @ClassName UserController.java
 * @createTime 2021年11月14日 22:36:00
 */
@Slf4j
@RestController
public class OrderController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    OrderService orderService;

    @Autowired
    DiscoveryClient discoveryClient;

    @Resource
    ProductService productService;
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info("下单{},调用商品微服务查询商品信息", pid);
        //ribbon负载均衡
        // Product product = restTemplate.getForObject("http://service-product/product/" + pid, Product.class);
        Product product = productService.product(pid);
        log.info("订单服务查询到商品{}信息，内容{}", pid, JSON.toJSONString(product));

        if (product.getPid() == -100) {
            Order order = new Order();
            order.setOid(-100L);
            order.setPname("下单失败");
            return order;
        }
        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");

        order.setPid(pid);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        log.info("下单信息{}", JSON.toJSONString(order));
        orderService.createOrder(order);

        //rocketmq
        log.info("发送消息{}", order);
        rocketMQTemplate.convertAndSend("order-tipic", order);
        return order;
    }


    /*public Order order(@PathVariable("pid") Integer pid) {
        log.info("下单{},调用商品微服务查询商品信息", pid);

        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        Product product = restTemplate.getForObject("http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + pid, Product.class);
        log.info("订单服务查询到商品{}信息，内容{}", pid, JSON.toJSONString(product));


        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");

        order.setPid(pid);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        log.info("下单信息{}", JSON.toJSONString(order));
        orderService.createOrder(order);
        return order;
    }*/

    /*public Order order(@PathVariable("pid") Integer pid) {
        log.info("下单{},调用商品微服务查询商品信息", pid);

        Product product = restTemplate.getForObject("http://localhost:8071/product/" + pid, Product.class);
        log.info("订单服务查询到商品{}信息，内容{}", pid, JSON.toJSONString(product));


        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");

        order.setPid(pid);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        log.info("下单信息{}", JSON.toJSONString(order));
        orderService.createOrder(order);
        return order;

    }*/
}
